package com.yuexun.dhdoctor.module.meetingNew.adapter;

import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.baselistener.OnItemPartClickListener;
import com.yuexun.dhdoctor.baselistener.OnItemPartClickListener02;
import com.yuexun.dhdoctor.module.meetingNew.bean.MeetingNewDateBean;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * Created by work-jx on 2016/11/15.
 */
public class MeetingNewMyListAdapter extends KJAdapter<MeetingNewDateBean> {

    private OnItemPartClickListener02 onItemPartClickListener;

    public void setOnItemPartClickListener(OnItemPartClickListener02 onItemPartClickListener) {
        this.onItemPartClickListener = onItemPartClickListener;
    }

    public MeetingNewMyListAdapter(AbsListView view, Collection<MeetingNewDateBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder adapterHolder, MeetingNewDateBean meetingBeanDate, boolean b, final int position) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date meetingDate = null;
        try {
            meetingDate = dateFormat.parse(meetingBeanDate.getStarttime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        adapterHolder.setText(R.id.tv_week, meetingBeanDate.getWeek());
        adapterHolder.setText(R.id.tv_snapshot_date, meetingDate == null ? "" : new SimpleDateFormat("MM月dd日").format(meetingDate));
        adapterHolder.setText(R.id.tv_meeting_theme, meetingBeanDate.getMeetingName());
        adapterHolder.setText(R.id.tv_meeting_location, "地点：" + meetingBeanDate.getRoomName());
        adapterHolder.setText(R.id.tv_meeting_creater, "发起人：" + meetingBeanDate.getCreatorName());
        TextView tvStatus = adapterHolder.getView(R.id.tv_status);
        TextView btnCreate = adapterHolder.getView(R.id.btn_create);
        btnCreate.setVisibility(View.VISIBLE);
//        adapterHolder.setText(R.id.tv_meeting_date,String.format("时间：", meetingBeanDate.getStarttime(), meetingBeanDate.getEndtime()));

        try {
            Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(meetingBeanDate.getStarttime());
            Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(meetingBeanDate.getEndtime());

            try {
                if (dateFormat.parse(meetingBeanDate.getStarttime()).equals(dateFormat.parse(meetingBeanDate.getEndtime()))) {
                    adapterHolder.setText(R.id.tv_meeting_date, "时间：" +
                            new SimpleDateFormat("MM-dd HH:mm").format(start) + " - " + new SimpleDateFormat("HH:mm").format(end));
                } else {
                    adapterHolder.setText(R.id.tv_meeting_date, "时间：" +
                            new SimpleDateFormat("MM-dd HH:mm").format(start) + " - " + new SimpleDateFormat("MM-dd HH:mm").format(end));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemPartClickListener.OnClick(position);
            }
        });

        if (meetingBeanDate.getMeetingFlag() == 1) {
            tvStatus.setText("正常");
            tvStatus.setTextColor(mCxt.getResources().getColor(R.color.main_text_01));
        } else if (meetingBeanDate.getMeetingFlag() == 2) {
            tvStatus.setText("取消");
            tvStatus.setTextColor(mCxt.getResources().getColor(R.color.red));
        } else if (meetingBeanDate.getMeetingFlag() == 3) {
            tvStatus.setText("提前");
            tvStatus.setTextColor(mCxt.getResources().getColor(R.color.yellow));
        } else if (meetingBeanDate.getMeetingFlag() == 4) {
            tvStatus.setText("延迟");
            tvStatus.setTextColor(mCxt.getResources().getColor(R.color.green));
        }
    }
}
